<?php

use yii\db\Migration;

class m180902_001743_create_agent extends Migration
{
    public function up()
    {
        $this->createTable('{{%agent}}', [
            'id' => $this->primaryKey(),
            'pid' => $this->integer()->comment('上级代理商编号'),
            'username' => $this->string(128)->notNull()->comment('登录用户名'),
            'password' => $this->string(512)->notNull()->comment('登录密码'),
            'auth_key' => $this->string(32)->comment('Cookie验证字符串'),
            'status' => $this->integer()->notNull()->comment('状态'),
            'create_time' => $this->integer()->notNull()->comment('创建时间'),
        ]);
        $this->createIndex('agent1_idx', '{{%agent}}', ['username']);
        $this->createIndex('fk_agent_agent1_idx', '{{%agent}}', ['pid']);
        try {
            $this->addForeignKey('fk_agent_agent1', '{{%agent}}', ['pid'], '{{%agent}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['agent_status', 1, '正常'],
            ['agent_status', 9, '暂停'],
            ['agent_status', 0, '删除'],
        ]);
    }

    public function safeDown()
    {
        $this->delete('{{%key_map}}', ['t' => 'agent_status']);
        $this->dropTable('{{%agent}}');
    }
}
